package com.lzy.shops.service.impl;

import com.alibaba.fastjson.JSON;
import com.lzy.shops.bean.Goods;
import com.lzy.shops.bean.PageBean;
import com.lzy.shops.bean.Search;
import com.lzy.shops.mapper.GoodsMapper;
import com.lzy.shops.service.GoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public PageBean<Goods> findAll(Map<String,Object> maps) {
        Integer pageSize = Integer.parseInt((String) maps.get("pageSize"));
        Integer pageNo = Integer.parseInt((String) maps.get("pageNo"));
        String search = (String) maps.get("search");
        HashMap<String,String> hashMap = JSON.parseObject(search, HashMap.class);
        Integer start = pageNo >= 1 ? (pageNo - 1) * pageSize : 1;
        Search searchBean = new Search(start,pageSize,hashMap);
        List<Goods> data = goodsMapper.findAll(searchBean);
        Integer countNums = goodsMapper.findCountNums();
        return new PageBean<Goods>(pageSize,pageNo,data,countNums);
    }

    @Override
    public Collection<Goods> findByGoodsId(Integer goodsId) {
        return goodsMapper.findByGoodsId(goodsId);
    }

    @Override
    public Integer addGoods(Goods goods) {
        return goodsMapper.addGoods(goods);
    }

    @Override
    public Integer deleteGoods(Integer goodsId) {
        return goodsMapper.deleteGoods(goodsId);
    }

    @Override
    public Integer updateGoods(Goods goods) {
        return goodsMapper.updateGoods(goods);
    }

    @Override
    public Goods getRandomGoods(String type) {
        Goods list;
        try {
            list = goodsMapper.getRandomGoods(type);
        } catch (Exception e) {
            list = new Goods();
        }
        return list;
    }

    @Override
    public List<Goods> findGoodsBySearch(String serach) {
        return goodsMapper.findGoodsBySearch(serach);
    }
}
